package com.xcustom.commerce.order.databeans;

import com.ibm.commerce.beans.SmartDataBeanImpl;
import  com.xcustom.commerce.order.databeans.ExtOrderXmlInfoDataBean;
import java.math.BigDecimal;
import java.util.Iterator;
import java.util.Locale;

import com.ibm.commerce.order.beansrc.ShipInfoDataBeanBase;
import com.ibm.commerce.marketing.promotion.reward.VoucherAdjustment;
import com.ibm.commerce.marketing.promotion.reward.ShippingUpgradeAtExtraCostAdjustment;
import com.ibm.commerce.marketing.promotion.reward.PercentOffVolumeDiscountAdjustment;
import com.ibm.commerce.marketing.promotion.reward.PercentOffShippingAdjustment;
import com.ibm.commerce.marketing.promotion.reward.PercentOffPriceAdjustment;
import com.ibm.commerce.marketing.promotion.reward.ItemUpgradeAtExtraCostAdjustment;
import com.ibm.commerce.marketing.promotion.reward.FreePurchasableGiftAdjustment;
import com.ibm.commerce.marketing.promotion.reward.FreeNonPurchasableGiftAdjustment;
import com.ibm.commerce.marketing.promotion.reward.FixedCostShippingAdjustment;
import com.ibm.commerce.marketing.promotion.reward.FixedCostAdjustment;
import com.ibm.commerce.marketing.promotion.reward.FixedAmountOffVolumeDiscountAdjustment;
import com.ibm.commerce.marketing.promotion.reward.FixedAmountOffShippingAdjustment;
import com.ibm.commerce.marketing.promotion.reward.FixedAmountOffPriceAdjustment;
import com.ibm.commerce.marketing.promotion.reward.FixedAmountOffAdjustment;
import com.ibm.commerce.marketing.promotion.reward.DefaultChoiceOfFreeGiftAdjustment;
import com.ibm.commerce.marketing.promotion.reward.CustomNumericValueAdjustment;
import com.ibm.commerce.marketing.promotion.reward.PercentOffAdjustment;
import org.w3c.dom.Node;
import com.ibm.commerce.marketing.promotion.reward.Adjustment;
import com.ibm.commerce.marketing.promotion.runtime.AssociatedOrderItem;
import com.ibm.commerce.marketing.promotion.runtime.LineItemSet;
import com.ibm.commerce.command.CommandContext;
import com.ibm.commerce.marketing.promotion.runtime.PromotionExecutionRecord;
import com.ibm.commerce.marketing.promotion.dependency.helper.XMLHelper;
import com.ibm.commerce.marketing.promotion.persistence.PersistenceObjectReadException;
import com.ibm.commerce.marketing.promotion.persistence.PersistenceObjectNotFoundException;
import com.ibm.commerce.marketing.promotion.dependency.OrderKey;
import com.ibm.commerce.marketing.promotion.runtime.PromotionArgumentSessionBeanPersistenceManager;
import com.ibm.commerce.marketing.promotion.runtime.PromotionArgumentPersistenceManager;
import com.ibm.commerce.marketing.promotion.runtime.PromotionArgument;
import com.ibm.commerce.tools.epromotion.databeans.RLProductDiscountDataBean;
import com.ibm.commerce.payments.plugincontroller.beans.PPCPayTranBean;

public class ExtOrderAdjustmentDataBean extends SmartDataBeanImpl{

	@Override
	public void populate() throws Exception {
		super.populate();
		
		System.out.println("El databean acaba de funcionar.........");
		
		 //Promocoes
		   Locale locale = Locale.getDefault();
		  
		   OrderKey orderKey = new OrderKey(new Long(19005));
			try {
				PromotionArgumentSessionBeanPersistenceManager pasbpm = new PromotionArgumentSessionBeanPersistenceManager();			
				PromotionArgument pa = pasbpm.load(orderKey);
				if(pa != null) {
					int index = -1;
					Iterator ite = pa.getPromotionExecutionRecords();
					
					if(ite != null) {
						while (ite.hasNext()) {
							PromotionExecutionRecord per = (PromotionExecutionRecord)ite.next();
							index++;
							Adjustment[] adjustments = per.getAdjustments();
							int descontoAplicadoPercentual = 0;
							if(adjustments != null) {
							
								for(int i = 0; i < adjustments.length; i++){
									
									Adjustment adjustment = adjustments[i];
									
									if(adjustment instanceof PercentOffAdjustment) {
										PercentOffAdjustment adjustmentType = (PercentOffAdjustment)adjustment;
									
										System.out.println("DescontoAplicadoPercentual" +adjustmentType.getMaxAmount()+ "--"+	adjustmentType.getAdjustmentType()+"--"+ adjustmentType.getCurrency());
									} else if(adjustment instanceof CustomNumericValueAdjustment) {
										CustomNumericValueAdjustment adjustmentType = (CustomNumericValueAdjustment)adjustment;
										System.out.println("NumericValue");
									}else if(adjustment instanceof DefaultChoiceOfFreeGiftAdjustment) {
										DefaultChoiceOfFreeGiftAdjustment adjustmentType = (DefaultChoiceOfFreeGiftAdjustment)adjustment;
										System.out.println("FreeGift");
									}else if(adjustment instanceof FixedAmountOffAdjustment) {
										FixedAmountOffAdjustment adjustmentType = (FixedAmountOffAdjustment)adjustment;
										System.out.println("AmountOff");
									}else if(adjustment instanceof FixedAmountOffPriceAdjustment) {
										FixedAmountOffPriceAdjustment adjustmentType = (FixedAmountOffPriceAdjustment)adjustment;
										
										System.out.println("AmountOff");
									}else if(adjustment instanceof FixedAmountOffShippingAdjustment) {
										FixedAmountOffShippingAdjustment adjustmentType = (FixedAmountOffShippingAdjustment)adjustment;
										System.out.println("AmountOff");
									}else if(adjustment instanceof FixedAmountOffVolumeDiscountAdjustment) {
										FixedAmountOffVolumeDiscountAdjustment adjustmentType = (FixedAmountOffVolumeDiscountAdjustment)adjustment;
										
										System.out.println("AmountOff");
									}else if(adjustment instanceof FixedCostAdjustment) {
										FixedCostAdjustment adjustmentType = (FixedCostAdjustment)adjustment;	
										String fixedCost = adjustmentType.getFixedCost() != null ? adjustmentType.getFixedCost().toString() : "";
										System.out.println("FixedCost");
									}else if(adjustment instanceof FixedCostShippingAdjustment) {
										FixedCostShippingAdjustment adjustmentType = (FixedCostShippingAdjustment)adjustment;
										String fixedCost = adjustmentType.getFixedCost() != null ? adjustmentType.getFixedCost().toString() : "";
										System.out.println("FixedCost");
									}else if(adjustment instanceof FreeNonPurchasableGiftAdjustment) {
										FreeNonPurchasableGiftAdjustment adjustmentType = (FreeNonPurchasableGiftAdjustment)adjustment;							
										System.out.println("FreeGift");
									}else if(adjustment instanceof FreePurchasableGiftAdjustment) {
										FreePurchasableGiftAdjustment adjustmentType = (FreePurchasableGiftAdjustment)adjustment;
										System.out.println("FreeGift");
									}else if(adjustment instanceof ItemUpgradeAtExtraCostAdjustment) {
										ItemUpgradeAtExtraCostAdjustment adjustmentType = (ItemUpgradeAtExtraCostAdjustment)adjustment;
										System.out.println("ExtraCost");
									} else if(adjustment instanceof PercentOffPriceAdjustment) {
										PercentOffPriceAdjustment adjustmentType = (PercentOffPriceAdjustment)adjustment;
										System.out.println("Percent");
									}else if(adjustment instanceof PercentOffShippingAdjustment) {
										PercentOffShippingAdjustment adjustmentType = (PercentOffShippingAdjustment)adjustment;
										System.out.println("Percent");
									}else if(adjustment instanceof PercentOffVolumeDiscountAdjustment) {
										PercentOffVolumeDiscountAdjustment adjustmentType = (PercentOffVolumeDiscountAdjustment)adjustment;
										//TODO: ver que valor va
										System.out.println("PercentOffVolumeDiscountAdjustment");
									}else if(adjustment instanceof ShippingUpgradeAtExtraCostAdjustment) {
										ShippingUpgradeAtExtraCostAdjustment adjustmentType = (ShippingUpgradeAtExtraCostAdjustment)adjustment;
										System.out.println("ExtraCost");
									}else if(adjustment instanceof VoucherAdjustment) {
										VoucherAdjustment adjustmentType = (VoucherAdjustment)adjustment;
										System.out.println("Coupon");
									} 
								}
							
							
						}
		
				}
			}
		
		
		
		
		
	}
	
	
	
}catch (PersistenceObjectNotFoundException e) {
	e.printStackTrace();
} catch (PersistenceObjectReadException e) {
	e.printStackTrace();
}catch (Exception e) {  e.printStackTrace(); }
			}
	}
